/*
 * ServerAddrCCU.cpp
 *
 *  Created on: 2013. 10. 22.
 *      Author: the2
 */

#include "ServerAddrCCU.h"
#include <algorithm>

#include "../engine.h"
#include "../Config.h"
#include "../Proxy.h"
#include "../NLogic/Static.h"


namespace ProjectZ {
namespace NLogic {

int ServerAddrCCU::Data::GetChannelData(int channel_id)
{
	switch(channel_id)
	{
	case CH1 :
		return _ch1;
	case CH2 :
		return _ch2;
		//test code
	case CH3 :
		return _ch3;
	case CH4 :
		return _ch4;
	case CH5 :
		return _ch5;
	case CH6 :
		return _ch6;
	case CH7 :
		return _ch7;
	case CH8 :
		return _ch8;
	case CH9 :
		return _ch9;
	case CH10 :
		return _ch10;
	case CH11 :
		return _ch11;
	case CH12 :
		return _ch12;
	case CH13 :
		return _ch13;
	case CH14 :
		return _ch14;
	case CH15 :
		return _ch15;
	case CH16 :
		return _ch16;
	case CH17 :
		return _ch17;
	case CH18 :
		return _ch18;
	case CH19 :
		return _ch19;
	case CH20 :
		return _ch20;
	}
	return 0;
}

ServerAddrCCU::ServerAddrCCU() {
	// TODO Auto-generated constructor stub

}

ServerAddrCCU::~ServerAddrCCU() {
	// TODO Auto-generated destructor stub
}


std::vector<std::pair<int,int>>
ServerAddrCCU::MapGreaterSort(std::map<int,int> &map)
{
	std::vector<std::pair<int,int> > vec(map.begin(), map.end());
	std::sort(vec.begin(), vec.end(), compare_pair_second<std::greater>());

	return vec;
}

bool
ServerAddrCCU::SetServerAddr()
{
//	int Value[20] = {-1, };
//
//	Static::SPACETREE & worldSpaceTree = NLogic::Static::Instance().GETWORLDSPACETREE();
//	std::for_each(worldSpaceTree.begin(), worldSpaceTree.end(), [&](Static::SPACETREE::value_type & pair) {
//		Value[pair.second->GetData()._index] = pair.second->GetCCU();
//	} );

	//DEBUGLOG("");


//	std::stringstream stream;
//	stream << '|';
//	Static::SPACETREE & worldSpaceTree = NLogic::Static::Instance().GETWORLDSPACETREE();
//	std::for_each(worldSpaceTree.begin(), worldSpaceTree.end(), [&](Static::SPACETREE::value_type & pair) {
//		stream << pair.second->GetCCU();
//		stream << '|';
//	} );


	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	auto ccuInstance = NLogic::Static::InstancePtr();
	clsAuto->SetQueryType(::Database::Command::S_PROCEDURE);
	clsAuto->MakeQuery("call sp_reload_serveraddr_ccu(%d, %d,"
			"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d,"
			"%d, %d, %d, %d, %d, %d, %d, %d, %d, %d)",
			::ProjectZ::Config::Instance().GetResourceDatabase()._channelUniqueIndex,
			 ::ProjectZ::NProxy::GetUserTree().size(),
			  ccuInstance->GETCCU(1), ccuInstance->GETCCU(2), ccuInstance->GETCCU(3), ccuInstance->GETCCU(4), ccuInstance->GETCCU(5),
			  ccuInstance->GETCCU(6), ccuInstance->GETCCU(7), ccuInstance->GETCCU(8), ccuInstance->GETCCU(9), ccuInstance->GETCCU(10),
			  ccuInstance->GETCCU(11), ccuInstance->GETCCU(12), ccuInstance->GETCCU(13), ccuInstance->GETCCU(14), ccuInstance->GETCCU(15),
			  ccuInstance->GETCCU(16), ccuInstance->GETCCU(17), ccuInstance->GETCCU(18), ccuInstance->GETCCU(19), ccuInstance->GETCCU(20)
	);

	if (false == ::Database::GETSESSION()->SyncExecute(static_cast<int>(DatabaseType::LOG_MASTER), clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;


	::Toolkit::Str<20> ip;

	this->_clsServerAddrTree.clear();
//	this->_clsCCUTree.clear();
	while( clsAuto->Next() )
	{
		::ProjectZ::NLogic::ServerAddrCCU::Data data;

		clsAuto->GetRecord() >> data._serverKey >> data._domainName >> data._port >> data._isActive >> data._ccu
				>> data._ch1 >> data._ch2 >> data._ch3 >> data._ch4 >> data._ch5
				>> data._ch6 >> data._ch7 >> data._ch8 >> data._ch9 >> data._ch10
				>> data._ch11 >> data._ch12 >> data._ch13 >> data._ch14 >> data._ch15
				>> data._ch16 >> data._ch17 >> data._ch18 >> data._ch19 >> data._ch20
				>> data._timeStamp;

		this->_clsServerAddrTree[data._serverKey] = data;
//		this->_clsCCUTree[data._serverKey] = data._ccu;
	}

//	this->_clsCCUVec = this->MapGreaterSort(this->_clsCCUTree);

	return true;
}

} /* namespace NLogic */
} /* namespace ProjectZ */
